local extension = Package("heroes_ev6")
extension.extensionName = "evolution"
--extension.game_modes_whitelist = {"brokenroyal_mode"}

local EV = require "packages/evolution/EV"
local U = require "packages/utility/utility"

Fk:loadTranslationTable{
  ["heroes_ev6"] = "新约角色21-24",
  ["ev"] = "新约",
}

local nuoaier = General(extension, "ev__nuoaier", "ev", 4, 4, General.Female)

local huxinkai = fk.CreateTriggerSkill{
  name = "huxinkai",
  mute = true,
  anim_type = "defensive",
  events = {fk.TargetConfirmed, fk.CardUseFinished},
  can_trigger = function(self, event, target, player, data)
    if event == fk.TargetConfirmed then
      if player:hasSkill(self) and data.card.trueName == "slash" then
      local n = player:getMark("@dasaochu")
      if n == 0 then n = player:getAttackRange() end
      return (player == target or (EV.isFriend(player, target) and player:distanceTo(target) <= n)) end
    else
      return player:hasSkill(self) and data.card.trueName == "slash" and data.extra_data and data.extra_data.show
      and data.damageDealt
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.TargetConfirmed then
      local unshown = table.filter(player:getCardIds(Player.Hand), function(id) return Fk:getCardById(id):getMark("@@shown-inhand") == 0 end)
      if #unshown > 0 then
        local card = player.room:askForCard(player, 1, 1, false, self.name, true, tostring(Exppattern{ id = unshown }), "#huxinkai-shown")
        if #card > 0 then
          self.cost_data = card[1]
          return true
        end
      end
    else
      local friends = table.map(table.filter(player.room.alive_players, function(p) return EV.isFriend(player, p) and p:isWounded() end), Util.IdMapper)
        local tos = player.room:askForChoosePlayers(player, friends, 1, 1, "#huxinkai-choose", self.name, true)
        if #tos > 0 then
          self.cost_data = tos[1]
          return true
        end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.TargetConfirmed then
      EV.OpenCards(room, player, self.cost_data, self.name)
      room:notifySkillInvoked(player, self.name)
      if Fk:getCardById(self.cost_data):compareSuitWith(data.card) then
        data.extra_data = data.extra_data or {}
        data.extra_data.huxinkai = target.id
        player:broadcastSkillInvoke(self.name, 1)
      end
    elseif event == fk.CardUseFinished then
      room:notifySkillInvoked(player, self.name, "support")
      player:broadcastSkillInvoke(self.name, 2)
      room:recover({
        who = player.room:getPlayerById(self.cost_data),
        num = 1,
        recoverBy = player,
        skillName = self.name
      })
    end
  end,

  refresh_events = {fk.PreCardUse, fk.DamageCaused},
  can_refresh = function(self, event, target, player, data)
    if player:hasSkill(self) and data.card and data.card.trueName == "slash" then
      if event == fk.PreCardUse then
        if target == player then
          if data.card:isVirtual() then
            return table.every(data.card.subcards, function (id) return Fk:getCardById(id):getMark("@@shown-inhand") > 0 end)
          else
            return data.card:getMark("@@shown-inhand") > 0
          end
        end
      else
        local e = player.room.logic:getCurrentEvent():findParent(GameEvent.CardEffect)
        return e and e.data[1].extra_data and e.data[1].extra_data.huxinkai
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    if event == fk.PreCardUse then
      --player.room:addCardMark(data.card, "huxinkai_buff")
      data.extra_data = data.extra_data or {}
      data.extra_data.show = true
    else
      local e = player.room.logic:getCurrentEvent():findParent(GameEvent.CardEffect)
      if not e then return end
      local use = e.data[1]
      if use.extra_data.huxinkai == data.to.id then
        data.damage = data.damage - 1
      end
    end
  end,
}

local dasaochu = fk.CreateActiveSkill{
  name = "dasaochu",
  anim_type = "offensive",
  prompt = "你可令攻击范围等于明置手牌数并将所有明置手牌当杀",
  target_num = 0,
  card_num = 0,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local shown = table.filter(player:getCardIds(Player.Hand), function(id) return Fk:getCardById(id):getMark("@@shown-inhand") > 0 end)
    room:setPlayerMark(player, "@dasaochu", #shown)
    room:addPlayerMark(player, "dasaochu_buff")
    if #shown > 0 then
      local slash = Fk:cloneCard("slash")
      local targets = table.filter(room:getOtherPlayers(player), function(p) return player:canUseTo(slash, p) end)
      targets = table.map(targets, Util.IdMapper)
      if #targets > 0 then
        local tos = room:askForChoosePlayers(player, targets, 1, 99, "#dasaochu-choose", self.name, false)
        if #tos > 0 then
          room:useVirtualCard("ev__slash", shown, player, table.map(tos, Util.Id2PlayerMapper), self.name, false)
        end
      end
    end
  end,
}

local dasaochu_attackrange = fk.CreateAttackRangeSkill{
  name = "#dasaochu_attackrange",
  fixed_func = function (self, from)
    if from:getMark("@dasaochu") > 0 then
      return from:getMark("@dasaochu")
    end
  end,
}

local dasaochu_trigger = fk.CreateTriggerSkill{
  name = "#dasaochu_trigger",
  refresh_events = {fk.TurnStart},
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(self) and target == player and player:getMark("dasaochu_buff") > 0
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "dasaochu_buff", 0)
    player.room:setPlayerMark(player, "@dasaochu", 0)
  end,
}

local dasaochu_prohibit = fk.CreateProhibitSkill{--锁攻击范围优先级低，限制使用杀的目标
  name = "#dasaochu_prohibit",
  is_prohibited = function(self, from, to, card)
    if card.trueName == "slash" and from:hasSkill(self) and from:getMark("dasaochu_buff") > 0 then
      return from:distanceTo(to) > from:getMark("@dasaochu")
    end
  end,
}

dasaochu:addRelatedSkill(dasaochu_attackrange)
dasaochu:addRelatedSkill(dasaochu_trigger)
--dasaochu:addRelatedSkill(dasaochu_prohibit)
nuoaier:addSkill(huxinkai)
nuoaier:addSkill(dasaochu)

Fk:loadTranslationTable{
  ["ev__nuoaier"] = "诺艾尔",
  ["designer:ev__nuoaier"] = "zengyouyu",
  ["huxinkai"] = "护心铠",
  [":huxinkai"] = "当你或你攻击范围内的己方角色成为【杀】的目标后，你可明置一张手牌，若这两张牌的花色相同，此【杀】对其造成的伤害-1。当你使用的明置【杀】结算结束后，若此【杀】造成过伤害，你可令一名己方角色回复1点体力。",
  ["#huxinkai-shown"] = "护心铠：你可明置一张手牌",
  ["#huxinkai-choose"] = "你可令一名己方角色回复1点体力",
  ["dasaochu"] = "大扫除",
  ["@dasaochu"] = "大扫除",
  [":dasaochu"] = "阶段技，你可令你的攻击范围至少为你此时的明置手牌数直至你的下回合开始；然后你将所有明置手牌当【杀】使用，此【杀】可额外选择任意个目标。",
  --<br> .."<font color='gray'>注：官方规则锁定攻击范围优先级低于武器，这里仅限制使用杀的目标合法性，无法处理与其他攻击范围相关技能交互</font>",
  ["#dasaochu-choose"] = "请选择【杀】的目标",

  ["$huxinkai1"] = "一定会保护你。",
  ["$huxinkai2"] = "别动，小心伤口，我来治疗！",
  ["$dasaochu1"] = "一定要干净利落。",
  ["$dasaochu2"] = "该打扫战场了。",
  ["$dasaochu3"] = "我会注意骑士的风度。",
  ["$dasaochu4"] = "我会注意女仆的礼仪。",
  ["~ev__nuoaier"] = "我还想。。保护大家。。",
}

local Shining = General(extension, "ev__shining", "ev", 4, 4, General.Female)

local jiaotiaolichang = fk.CreateViewAsSkill{
  name = "jiaotiaolichang",
  anim_type = "offensive",
  pattern = "doctrinal_proclama",
  card_filter = function(self, to_select, selected)
    return #selected == 0 and Fk:currentRoom():getCardArea(to_select) == Card.PlayerHand
  end,
  view_as = function(self, cards)
    if #cards ~= 1 then return end
    local card = Fk:cloneCard("doctrinal_proclama")
    card.skillName = self.name
    card:addSubcard(cards[1])
    return card
  end,
  enabled_at_play = function(self, player)
    return player:getMark("jiaotiaolichang-turn") == 0
  end,
}

local jiaotiaolichang_trigger = fk.CreateTriggerSkill{
  name = "#jiaotiaolichang_trigger",
  mute = true,
  events = {fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      for _, move in ipairs(data) do
        if move.to == player.id and move.toArea == Player.Hand and move.skillName == "jiaotiaolichang" then
          return true
        end
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:addPlayerMark(player, "jiaotiaolichang-turn")
    local players = table.map(table.filter(room.alive_players, function (p) return p:isWounded() end), Util.IdMapper)
    local tos, cards = player.room:askForChooseCardsAndPlayers(player, 1, 1, players, 1, 1,
    ".|.|heart,diamond|hand", "#jiaotiaolichang", self.name, true, true)
   if #tos > 0 and #cards > 0 then
      room:moveCardTo(cards, Card.DiscardPile, nil, fk.ReasonDiscard, self.name, "", true)
      local to = room:getPlayerById(tos[1])
      room:recover({
        who = to,
        num = 1,
        recoverBy = player,
        skillName = self.name
      })
   end
  end,
}

local emobihu = fk.CreateTriggerSkill{
  name = "emobihu",
  anim_type = "defensive",
  events = {fk.TargetConfirmed},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and data.card.trueName == "slash"
    and (player == target or (EV.isFriend(player, target) and player:inMyAttackRange(target)))
  end,
  on_cost = function(self, event, target, player, data)
    local hands = table.filter(target:getCardIds(Player.Hand), function (id)
      return Fk:getCardById(id).trueName == "jink" or (Fk:getCardById(id).suit == data.card.suit and Fk:getCardById(id):getMark("@@shown-inhand") > 0)
    end)
    local cards = player.room:askForCard(target, 1, 1, true, self.name, true, tostring(Exppattern{ id = hands }), "#emobihu")
    if #cards > 0 then
      self.cost_data = cards[1]
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:moveCardTo(self.cost_data, Card.DiscardPile, nil, fk.ReasonDiscard, self.name, "", true)
    data.extra_data = data.extra_data or {}
    data.extra_data.emobihu = target.id
  end,

  refresh_events = {fk.DamageCaused},
  can_refresh = function(self, event, target, player, data)
    if player:hasSkill(self) and data.card and data.card.trueName == "slash" then
      local e = player.room.logic:getCurrentEvent():findParent(GameEvent.CardEffect)
      return e and e.data[1].extra_data and e.data[1].extra_data.emobihu
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local e = player.room.logic:getCurrentEvent():findParent(GameEvent.CardEffect)
    if not e then return end
    local use = e.data[1]
    if use.extra_data.emobihu == data.to.id then
      data.damage = data.damage - 1
    end
  end,
}

jiaotiaolichang:addRelatedSkill(jiaotiaolichang_trigger)
Shining:addSkill(jiaotiaolichang)
Shining:addSkill(emobihu)

Fk:loadTranslationTable{
  ["ev__shining"] = "闪灵",
  ["designer:ev__shining"] = "zengyouyu",
  ["jiaotiaolichang"] = "教条立场",
  [":jiaotiaolichang"] = "出牌阶段，若你于本回合内未以此法获得过牌，你可将一张手牌当【教义宣示】使用；当你以此法获得牌后，你可弃置一张红色手牌，令一名角色回复1点体力。",
  ["#jiaotiaolichang"] = "你可弃置一张红色手牌，令一名角色回复1点体力",
  --["#ev__mingjia-return"] = "你可将一张牌交给%src",
  ["emobihu"] = "恶魔庇护",
  [":emobihu"] = "当你或你攻击范围内的己方角色成为【杀】的目标后，其可弃置一张【闪】或与此【杀】花色相同的明置手牌，令此【杀】对其造成的伤害-1。",
  ["#emobihu"] = "你可弃置一张【闪】或与此【杀】花色相同的明置手牌",

  ["$jiaotiaolichang1"] = "只是用于愈合伤口的力量，是不够的。",
  ["$jiaotiaolichang2"] = "单单作为医者，是无法拯救更多人的。",
  ["$emobihu1"] = "你，由我来守护。",
  ["$emobihu2"] = "别担心。",
  ["~ev__shining"] = "胜利，失败，只不过是无限循环的一瞬。",
}


local shenlilinghua = General(extension, "ev__shenlilinghua", "ev", 4, 4, General.Female)

local ev__mingjia = fk.CreateTriggerSkill{
  name = "ev__mingjia",
  anim_type = "control",
  mute = true,
  events = {fk.TargetConfirming},
  can_trigger = function(self, event, target, player, data)
    local origin_targets = U.getActualUseTargets(player.room, data, event)
    return player:hasSkill(self.name) and #origin_targets > 1 and (target == player or (data.from == player.id and data.firstTarget))
  end,
  on_cost = function(self, event, target, player, data)
    local origin_targets = U.getActualUseTargets(player.room, data, event)
    table.removeOne(origin_targets, player.id)
    local tos, cards = player.room:askForChooseCardsAndPlayers(player, 1, 1, origin_targets, 1, 1,
     ".|.|.|.|.|^basic", "#ev__mingjia-give", self.name, true, true)
    if #tos > 0 and #cards > 0 then
      self.cost_data = {tos[1], cards[1]}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = player.room:getPlayerById(self.cost_data[1])
    room:notifySkillInvoked(player, self.name)
    if to.general == "ev__xiaogong" then
      player:broadcastSkillInvoke(self.name, 3)
    else
      if EV.isFriend(player, to) then
        player:broadcastSkillInvoke(self.name, 1)
      else
        player:broadcastSkillInvoke(self.name, 2)
      end
    end
    room:moveCardTo(self.cost_data[2], Card.PlayerHand, to, fk.ReasonGive, self.name, nil, false, player.id)
    AimGroup:cancelTarget(data, self.cost_data[1])
    local card = room:askForCard(to, 1, 1, true, self.name, true, ".|.|.|.|.|.|^"..tostring(self.cost_data[2]), "#ev__mingjia-return:"..player.id)
    if #card > 0 then
      room:moveCardTo(card[1], Card.PlayerHand, player, fk.ReasonGive, self.name, nil, false, self.cost_data[1])
    end
  end,
}

local ev__bailu = fk.CreateActiveSkill{
  name = "ev__bailu",
  anim_type = "offensive",
  prompt = "#ev__bailu",
  mute = true,
  target_num = 1,
  card_num = function(self)
    return Self:usedSkillTimes(self.name, Player.HistoryPhase) + 1
  end,
  can_use = function(self, player)
    return not player:isKongcheng()
  end,
  card_filter = function(self, to_select, selected)
    if Fk:currentRoom():getCardArea(to_select) == Card.PlayerHand and Fk:getCardById(to_select):getMark("@@shown-inhand") == 0 then
      if #selected > 0 then
        return Fk:getCardById(to_select).color == Fk:getCardById(selected[1]).color
      else
        return true
      end
    end
  end,
  target_filter = function(self, to_select, selected)
    return #selected == 0
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    room:notifySkillInvoked(player, self.name)
    player:showCards(effect.cards)
    player:broadcastSkillInvoke(self.name, math.min(player:usedSkillTimes(self.name, Player.HistoryPhase), 3))
    if player:usedSkillTimes(self.name, Player.HistoryPhase) == 1 then
      local mark = player:getTableMark("ev__mingjia-turn")
      table.insertIfNeed(mark, effect.tos[1])
      room:setPlayerMark(player, "ev__mingjia-turn", mark)
    elseif player:usedSkillTimes(self.name, Player.HistoryPhase) == 2 then
      room:setPlayerMark(player, "BypassTimesLimit", 1)
      U.askForUseVirtualCard(room, player, "ev_ice__slash", nil, self.name)
      room:setPlayerMark(player, "BypassTimesLimit", 0)
    elseif player:usedSkillTimes(self.name, Player.HistoryPhase) > 2 then
      --U.askForUseVirtualCard(room, player, "snow_storm", nil, self.name, "#ev__bailu_snowstorm")
      room:useVirtualCard("snow_storm", effect.cards, player, room:getOtherPlayers(player), self.name, false)
    end
  end,
}

local bailu_distance = fk.CreateDistanceSkill{
  name = "#ev__bailu_distance",
  correct_func = function(self, from, to) return 0 end,
  fixed_func = function(self, from, to)
    local mark = from:getTableMark("ev__mingjia-turn")
    if table.contains(mark, to.id) then
      return 1
    end
  end,
}

ev__bailu:addRelatedSkill(bailu_distance)
shenlilinghua:addSkill(ev__mingjia)
shenlilinghua:addSkill(ev__bailu)

Fk:loadTranslationTable{
  ["ev__shenlilinghua"] = "神里绫华",
  ["designer:ev__shenlilinghua"] = "陆丿伯言",
  ["ev__mingjia"] = "名家风雅",
  [":ev__mingjia"] = "当你使用牌指定目标时，或成为牌的目标时，若此牌的目标角色数不小于2，你可将一张非基础牌交给一名目标角色并取消其，其可将另一张牌交给你。",
  ["#ev__mingjia-give"] = "可将一张非基础牌交给一名目标角色并取消其",
  ["#ev__mingjia-return"] = "你可将一张牌交给%src",
  ["ev__bailu"] = "白鹭霜华",
  [":ev__bailu"] = "出牌阶段，你可选择一名角色并展示X张颜色相同的手牌，若X：为1，你与其的距离于本回合内视为1；为2，你视为使用冰【杀】；大于2，你将这些牌当【暴风雪】使用（X为你于本回合内发动过此技能的次数+1）。",
  ["#ev__bailu"] = "你可选择一名角色并展示任意张颜色相同的手牌",

  ["$ev__mingjia1"] = "刀剑抱业，名工怀宝。",
  ["$ev__mingjia2"] = "若知是梦何须醒，不比真如一相会。",
  ["$ev__mingjia3"] = "（选择宵宫）宵宫尝尝晶螺糕吧。",
  ["$ev__bailu1"] = "起舞吧。",
  ["$ev__bailu2"] = "樱吹雪。",
  ["$ev__bailu3"] = "神里流，霜灭！",
  ["~ev__shenlilinghua"] = "我还有。。未竟之事。。",
}

local shenlilingren = General(extension, "ev__shenlilingren", "ev", 4)

Fk:addPoxiMethod{
  name = "ev__longhui",
  card_filter = function(to_select, selected, data)
    local s = Fk:getCardById(to_select).color
    for _, id in ipairs(selected) do
      if Fk:getCardById(id).color == s then return false end
    end
    return Fk:getCardById(to_select):getMark("@@shown-inhand") == 0
  end,
  feasible = function(selected, data)
    return #selected == 2
  end,
  prompt = "泷廻鉴花：你可明置两张颜色不同的手牌"
}

local ev__longhui = fk.CreateTriggerSkill{
  name = "ev__longhui",
  anim_type = "offensive",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self.name) and player.phase == Player.Play and player:getHandcardNum() > 1
  end,
  on_cost = function(self, event, target, player, data)
    local cards = player.room:askForPoxi(player, "ev__longhui", {
      { "$Hand", player:getCardIds(Player.Hand) },
    })
    if #cards > 0 then
      self.cost_data = cards
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    EV.OpenCards(room, player, self.cost_data, self.name)
    room:setPlayerMark(player, "BypassTimesLimit", 1)
    U.askForUseVirtualCard(room, player, "ev__slash", nil, self.name, "#ev__longhui", true, true, true)
    room:setPlayerMark(player, "BypassTimesLimit", 0)
    room:addPlayerMark(player, "ev__longhui-turn")
  end,
}

local ev__longhui_tar = fk.CreateTargetModSkill{
  name = "#ev__longhui_targetmod",
  distance_limit_func =  function(self, player, skill, card)
    if player:getMark("ev__longhui-turn") > 0 and card and card.trueName == "slash" and card:getMark("@@shown-inhand") > 0 then
      return 999
    end
  end,
}

local ev__longhui_prohibit = fk.CreateProhibitSkill{
  name = "#ev__longhui_prohibit",
  prohibit_use = function(self, player, card)
    return player:getMark("ev__longhui-turn") > 0 and card and card.type == Card.TypeTrick and card:getMark("@@shown-inhand") == 0
  end,
}

local ev__panji = fk.CreateTriggerSkill{
  name = "ev__panji",
  anim_type = "offensive",
  events = {fk.EventPhaseEnd},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) and player.phase == Player.Discard then
      local ids = {}
      player.room.logic:getEventsOfScope(GameEvent.MoveCards, 999, function(e)
        for _, move in ipairs(e.data) do
          if move.toArea == Card.DiscardPile and move.from == player.id and move.moveReason == fk.ReasonDiscard then
            for _, info in ipairs(move.moveInfo) do
              if (info.fromArea == Card.PlayerEquip or info.fromArea == Card.PlayerHand) and player.room:getCardArea(info.cardId) == Card.DiscardPile
              and Fk:getCardById(info.cardId).type == Card.TypeTrick then
                table.insertIfNeed(ids, info.cardId)
              end
            end
          end
        end
        return false
      end, Player.HistoryPhase)
      if #ids == 0 then return false end
      return true
    end
  end,
  on_cost = function(self, event, target, player, data)
    local to = player.room:askForChoosePlayers(player, table.map(player.room:getOtherPlayers(player), Util.IdMapper), 1, 1,
    "#ev__panji-choose", self.name, true)
    if #to > 0 then
      self.cost_data = to[1]
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local ids = {}
    player.room.logic:getEventsOfScope(GameEvent.MoveCards, 999, function(e)
      for _, move in ipairs(e.data) do
        if move.toArea == Card.DiscardPile and move.from == player.id and move.moveReason == fk.ReasonDiscard then
          for _, info in ipairs(move.moveInfo) do
            if (info.fromArea == Card.PlayerEquip or info.fromArea == Card.PlayerHand) and player.room:getCardArea(info.cardId) == Card.DiscardPile
            and Fk:getCardById(info.cardId).type == Card.TypeTrick then
              table.insertIfNeed(ids, info.cardId)
            end
          end
        end
      end
      return false
    end, Player.HistoryPhase)
    local to = room:getPlayerById(self.cost_data)
    local get = U.askforChooseCardsAndChoice(player, ids, {"OK"}, self.name, "请选择交给该角色的牌")
    if #get > 0 then
      local card = Fk:getCardById(get[1])
      room:moveCardTo(get[1], Player.Hand, to, fk.ReasonGive, self.name, nil, false, player.id)
      local choice = room:askForChoice(to, {"ev__panji1", "ev__panji2"}, self.name)
      if choice == "ev__panji1" then
        local tos = table.map(table.filter(room.alive_players, function(p) return to:canUseTo(card, p) end), Util.IdMapper)
        local victim = room:askForChoosePlayers(player, tos, 1, 1, "#ev__panji-target:::"..card:toLogString(), self.name, true)
        if #victim == 0 then
          choice = "ev__panji2"
        else
          local use = room:askForUseCard(to, card.name, tostring(Exppattern{ id = get }), "#ev__panji-use::"..victim[1], true,
          {must_targets = victim, bypass_distances = true, bypass_times = true})
          if use then
            room:addPlayerMark(player, "ev__panji-buff")
            room:useCard(use)
            room:setPlayerMark(player, "ev__panji-buff", 0)
          else
            choice = "ev__panji2"
          end
        end
      end
      if choice == "ev__panji2" then
        room:moveCardTo(card, Card.DiscardPile, nil, fk.ReasonDiscard, self.name, "", true, self.cost_data)
        if not to:isNude() then
          local cards = room:askForCardsChosen(player, to, 1, 1, "he", self.name)
          room:moveCardTo(cards, Player.Hand, player, fk.ReasonPrey, self.name, nil, false, player.id)
        end
      end
    end
  end,

  refresh_events = {fk.CardEffecting},
  can_refresh = function(self, event, target, player, data)
    return data.from ~= player.id and player:hasSkill(self.name) and player:getMark("ev__panji-buff") > 0
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "ev__panji-buff", 0)
    player:drawCards(1, self.name)
  end, 
}

ev__longhui:addRelatedSkill(ev__longhui_tar)
ev__longhui:addRelatedSkill(ev__longhui_prohibit)
shenlilingren:addSkill(ev__longhui)
shenlilingren:addSkill(ev__panji)


Fk:loadTranslationTable{
  ["ev__shenlilingren"] = "神里绫人",
  ["designer:ev__shenlilingren"] = "子规枝上",
  ["ev__longhui"] = "泷廻鉴花",
  [":ev__longhui"] = "出牌阶段开始时，你可明置两张颜色不同的手牌，视为使用【杀】（无距离限制）；若如此做，你于本阶段内：使用明置【杀】无距离限制，且不能使用暗置魔法牌。",
  ["#ev__longhui"] = "请选择视为使用【杀】的目标角色",
  ["#ev__longhui_prohibit"] = "泷廻鉴花",
  ["hozen"] = "符",
  ["ev__panji"] = "磐祭叶守",
  [":ev__panji"] = "弃牌阶段结束时，你可将一张于此阶段内弃置的魔法牌交给一名其他角色，令其选择一项：1.你选择其使用此牌的合法目标，其对目标使用此牌，你于此牌生效时摸一张牌；2.弃置此牌，然后你获得其一张牌。",
  ["#ev__panji-choose"] = "你可选择一名角色，然后将一张于此阶段内弃置的魔法牌交给该角色",
  ["ev__panji1"] = "使用此牌",
  ["ev__panji2"] = "弃置此牌",
  ["#ev__panji-target"] = "请选择%arg的目标角色",
  ["#ev__panji-use"] = "请对%dest使用此牌",

  ["$ev__longhui1"] = "秋水三尺。",
  ["$ev__longhui2"] = "神里流…水囿！",
  ["$ev__panji1"] = "对你有用便好。",
  ["$ev__panji2"] = "原来你需要这些吗？",
  ["~ev__shenlilingren"] = "百密终是有一疏么…",

}
local mirror = General(extension, "ev__mirror", "ev", 3, 3, General.Female)

local ev__zhujing = fk.CreateTriggerSkill{
  name = "ev__zhujing",
  events = {fk.CardUseFinished},
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self.name) and (data.card.type == Card.TypeBasic or data.card:isCommonTrick())
    and player:usedSkillTimes(self.name, Player.HistoryTurn) == 0 then
      local suits = {}
      local centers = {}
      for _, id in ipairs(player.player_cards[Player.Hand]) do
        if Fk:getCardById(id).suit ~= Card.NoSuit then
          table.insertIfNeed(suits, Fk:getCardById(id).suit)
        end
      end
      for _, id in ipairs(EV.getCenterArea(player.room)) do
        if Fk:getCardById(id).suit ~= Card.NoSuit then
          table.insertIfNeed(centers, Fk:getCardById(id).suit)
        end
      end
      return #suits == #centers
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:notifySkillInvoked(player, self.name, "special")
    player:broadcastSkillInvoke(self.name)
    U.askForUseVirtualCard(player.room, player, data.card.name, nil, self.name, "#ev__zhujing:::"..data.card.name, true, true, false, true)
  end,
  refresh_events = {fk.Damage},
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill(self.name) and data.card and data.card.skillName == self.name
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:recover({
      who = player,
      num = 1,
      recoverBy = player,
      skillName = self.name
    })
  end,
}

local ev__jianying = fk.CreateActiveSkill{
  name = "ev__jianying",
  anim_type = "offensive",
  prompt = "请选择一张手牌和两名你至其距离相同角色",
  target_num = 2,
  card_num = 1,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_filter = function(self, to_select, selected)
    return #selected == 0 and Fk:currentRoom():getCardArea(to_select) == Card.PlayerHand
  end,
  target_filter = function (self, to_select, selected, _, _, _, player)
    if #selected == 0 then
      return to_select ~= player.id and not Fk:currentRoom():getPlayerById(to_select):isNude()
    else
      local to = Fk:currentRoom():getPlayerById(selected[1])
      return player:distanceTo(to) == player:distanceTo(Fk:currentRoom():getPlayerById(to_select)) and not Fk:currentRoom():getPlayerById(to_select):isNude()
    end
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local color = Fk:getCardById(effect.cards[1]).color
    room:moveCardTo(effect.cards, Card.DiscardPile, nil, fk.ReasonDiscard, self.name, nil, true, player.id)
    local cards = {}
    for _, p in ipairs(effect.tos) do
      local id = room:askForCardChosen(player, room:getPlayerById(p), "he", self.name)
      if id then
        table.insert(cards, id)
        room:moveCardTo(id, Card.DiscardPile, nil, fk.ReasonDiscard, self.name, nil, true, player.id)
      end
    end
    if #cards == 2 then
      local victim = nil
      if color == Card.Black then
        if Fk:getCardById(cards[1]).color == Card.Red and Fk:getCardById(cards[2]).color ~= Card.Red then
          victim = room:getPlayerById(effect.tos[1])
        elseif Fk:getCardById(cards[1]).color ~= Card.Red and Fk:getCardById(cards[2]).color == Card.Red then
          victim = room:getPlayerById(effect.tos[2])
        end
      elseif color == Card.Red then
        if Fk:getCardById(cards[1]).color == Card.Black and Fk:getCardById(cards[2]).color ~= Card.Black then
          victim = room:getPlayerById(effect.tos[1])
        elseif Fk:getCardById(cards[1]).color ~= Card.Black and Fk:getCardById(cards[2]).color == Card.Black then
          victim = room:getPlayerById(effect.tos[2])
        end
      end
      if not victim then return end
      if victim and room:askForSkillInvoke(player, self.name, nil, "#ev__jianying-damage:"..victim.id) then
        room:damage({
          from = player,
          to = victim,
          damage = 1,
          damageType = fk.NormalDamage,
          skillName = self.name,
        })
      end
    end
  end,
}
ev__zhujing.CenterArea = true
mirror:addSkill(ev__zhujing)
mirror:addSkill(ev__jianying)
Fk:loadTranslationTable{
  ["ev__mirror"] = "镜",
  ["designer:ev__mirror"] = "次氯酸",
  ["ev__zhujing"] = "铸镜",
  ["#ev__zhujing"] = "铸镜：你可视为使用【%arg】",
  [":ev__zhujing"] = "回合技，当你使用的【杀】或即时魔法牌结算结束后，若你手牌中含有的花色数等于当前回合内置入弃牌堆的牌含有的花色数，你可视为使用此牌；当你以此法使用牌造成伤害后，你回复1点体力。",
  ["ev__jianying"] = "见影",
  [":ev__jianying"] = "阶段技，你可弃置一张<font color=\"#6495ED\">黑色</font>/<font color=\"#D2042D\">红色</font>手牌，弃置两名你与其距离相同的角色的各一张牌；若仅有一名角色以此法失去了<font color=\"#6495ED\">红色</font>/<font color=\"#D2042D\">黑色</font>牌，你可对其造成1点伤害。",
  ["#ev__jianying-damage"] = "见影：你可对%src造成1点伤害",
  
  ["$ev__zhujing1"] = "阻拦在眼前的，统统打碎就行了。",
  ["$ev__zhujing2"] = "亿万片破碎的镜子，亿万个辉煌的太阳。",
  ["$ev__jianying1"] = "双重割裂！",
  ["$ev__jianying2"] = "粉碎他们！",
  ["~ev__mirror"] = "呃。。朝高处看。。",
}

local Psychologist = General(extension, "ev__psychologist", "ev", 3, 3, General.Female)

local ev__yiqing = fk.CreateActiveSkill{
  name = "ev__yiqing",
  anim_type = "support",
  card_num = 0,
  target_num = 1,
  prompt = "#ev__yiqing",
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function (self, to_select, selected, _, _, _, player)
    return #selected == 0 and to_select ~= player.id and Fk:currentRoom():getPlayerById(to_select):isWounded()
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    room:recover({
      who = target,
      num = 1,
      recoverBy = player,
      skillName = self.name
    })
    room:loseHp(player, 1, self.name)
    local n = player:usedSkillTimes(self.name, Player.HistoryGame)
    local cards = room:askForCard(target, n, n, true, self.name, true, ".|.|heart", "#ev__yiqing-give::"..effect.from..":"..n)
    if #cards > 0 then
      room:moveCardTo(cards, Card.PlayerHand, player, fk.ReasonGive, self.name, "", true)
      room:recover{ who = player, num = 1, skillName = self.name }
    end
  end,
}

local ev__yingji = fk.CreateTriggerSkill{
  name = "ev__yingji",
  events = {fk.Damaged},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self)
  end,
  on_cost = function(self, event, target, player, data)
    local card = player.room:askForCard(player, 1, 1, true, self.name, true, ".|.|heart", "#ev__yingji")
    if #card > 0 then
      self.cost_data = card[1]
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:recastCard(self.cost_data, player, self.name)
    if player:isWounded() then
      room:recover{ who = player, num = 1, skillName = self.name }
    end
    room:addPlayerMark(player, "@@ev__yingji")
  end,

  refresh_events = {fk.TurnStart},
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player:getMark("@@ev__yingji") > 0
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "@@ev__yingji", 0)
  end,
}

local yingji_prohibit = fk.CreateProhibitSkill{
  name = "#ev__yingji_prohibit",
  is_prohibited = function(self, from, to, card)
    return card.trueName == "peach" and to:getMark("@@ev__yingji") > 0
  end,
}
ev__yingji:addRelatedSkill(yingji_prohibit)
Psychologist:addSkill(ev__yiqing)
Psychologist:addSkill(ev__yingji)

Fk:loadTranslationTable{
  ["ev__psychologist"] = "心理学家",
  ["designer:ev__psychologist"] = "次氯酸",
  ["ev__yiqing"] = "移情",
  ["#ev__yiqing"] = "移情：你可令一名其他角色回复1点体力，然后你失去1点体力",
  [":ev__yiqing"] = "阶段技，你可令一名其他角色回复1点体力，然后你失去1点体力；若如此做，其可交给你X张红桃牌，令你回复1点体力（X为你发动过此技能的次数）。",
  ["#ev__yiqing-give"] = "移情：你可将%arg张红桃牌交给%dest，令其回复1点体力",
  ["ev__yingji"] = "应激",
  [":ev__yingji"] = "当你受到伤害后，你可重铸一张红桃牌，然后你回复1点体力；若如此做，直至你的下回合开始，你不是【茶】的合法目标。",
  ["#ev__yingji"] = "应激：你可重铸一张红桃牌并回复1点体力",
  ["@@ev__yingji"] = "应激",
  ["#ev__yingji_prohibit"] = "应激",
}

local bailishouyue = General(extension, "ev__bailishouyue", "ev", 4)

local kuangfengjuji = fk.CreateActiveSkill{
  name = "kuangfengjuji",
  anim_type = "offensive",
  card_num = 1,
  target_num = 1,
  prompt = "#kuangfengjuji",
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_filter = function(self, to_select, selected)
    return #selected == 0 and Fk:getCardById(to_select).trueName == "slash"
  end,
  target_filter = function (self, to_select, selected, _, _, _, player)
    return #selected == 0 and to_select ~= player.id
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    room:moveCardTo(effect.cards, Card.DiscardPile, nil, fk.ReasonDiscard, self.name, nil, true, player.id)
    local unshown = table.filter(target:getCardIds(Player.Hand), function(id) return Fk:getCardById(id):getMark("@@shown-inhand") == 0 end)
    if #unshown > 0 then
      target:showCards(unshown)
      if table.find(unshown, function(id) return Fk:getCardById(id).trueName == "jink" end) then
        room:damage({
          from = player,
          to = target,
          damage = 2,
          damageType = fk.NormalDamage,
          skillName = self.name,
        })
      end
    end
  end,
}


local jingmizhiyan = fk.CreateTriggerSkill{
  name = "jingmizhiyan",
  events = {fk.TurnEnd},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      local room = player.room
      local turn_event = room.logic:getCurrentEvent():findParent(GameEvent.Turn, true)
      if turn_event == nil then return false end
      return #U.getEventsByRule(room, GameEvent.UseCard, 1, function (e)
        local use = e.data[1]
        if use.from == player.id then
          return true
        end
      end, turn_event.id) > 0
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = table.map(table.filter(room.alive_players, function (p) return not p:isKongcheng() end), Util.IdMapper)
    local tos = room:askForChoosePlayers(player, targets, 1, 1, "#jingmizhiyan-choose", self.name, true)
    if #tos > 0 then
      self.cost_data = tos[1]
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(self.cost_data)
    local shown = table.filter(to:getCardIds(Player.Hand), function(id) return Fk:getCardById(id):getMark("@@shown-inhand") > 0 end)
    local all_choices = {"jingmizhiyan1::"..to.id, "jingmizhiyan2::"..to.id}
    local choices = {all_choices[1]}
    if #shown > 0 then table.insert(choices, all_choices[2]) end
    if #choices == 0 then return end
    local choice = room:askForChoice(player, choices, self.name, "", false, all_choices)
    if choice == all_choices[1] then
      U.viewCards(player, to:getCardIds(Player.Hand), self.name)
    else
      local id = player.room:askForCardChosen(player, to, {
        card_data = {
          {to.general, shown},
        }
      }, self.name)
      EV.ConcealCards(room, to, id, self.name, player)
    end
  end,
}


bailishouyue:addSkill(kuangfengjuji)
bailishouyue:addSkill(jingmizhiyan)

Fk:loadTranslationTable{
  ["ev__bailishouyue"] = "百里守约",
  ["designer:ev__bailishouyue"] = "次氯酸",
  ["kuangfengjuji"] = "狂风狙击",
  [":kuangfengjuji"] = "阶段技，你可弃置一张【杀】，令一名其他角色展示所有暗置手牌，若其中有【闪】，则你对其造成2点伤害。",
  ["#kuangfengjuji"] = "你可选择一张【杀】和一名其他角色",
  ["jingmizhiyan"] = "静谧之眼",
  [":jingmizhiyan"] = "回合技（终），若你于此回合内使用过牌，则你可选择一项：1.观看一名角色的手牌；2.暗置一名角色的一张手牌。",
  ["#jingmizhiyan-choose"] = "静谧之眼：请选择一名有手牌的角色",
  ["jingmizhiyan1"] = "观看%dest所有手牌",
  ["jingmizhiyan2"] = "暗置%dest一张手牌",

  ["$kuangfengjuji1"] = "给我一个目标，还你一片寂静。",
  ["$kuangfengjuji2"] = "关于取下敌人性命这件事，也从不失约。",
  ["$jingmizhiyan1"] = "暗中观察。",
  ["$jingmizhiyan2"] = "击穿盲点！",
  ["~ev__bailishouyue"] = "最后一次失约。。。",
}

local guiwangjiutun = General(extension, "ev__guiwangjiutun", "ev", 4)

local ev__dayaopower = fk.CreateTriggerSkill{
  name = "ev__dayaopower",
  anim_type = "defensive",
  frequency = Skill.Compulsory,
  events = {fk.DamageInflicted},
  can_trigger = function(self, event, target, player, data)
    return EV.isFriend(player, target) and player:hasSkill(self.name) and target.hp == 1 and data.card and data.card.name == "ev__slash"
  end,
  on_use = function(self, event, target, player, data)
    data.damage = data.damage - 1
  end,
}

local ev__dayaopower_slash = fk.CreateTargetModSkill{
  name = "#ev__dayaopower_targetmod",
  residue_func = function(self, player, skill, scope, card)
    local room = Fk:currentRoom()
    if player:getLostHp() == 1 and skill.trueName == "slash_skill" and card.trueName == "slash" and scope == Player.HistoryPhase then
      for _, p in ipairs(room.alive_players) do
        if EV.isFriend(p, player) and p:hasSkill("ev__dayaopower") then
          return 1
        end
      end
    end
  end,
}

local ev__tianhuo = fk.CreateTriggerSkill{
  name = "ev__tianhuo",
  anim_type = "offensive",
  frequency = Skill.Compulsory,
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player.phase == Player.Play
  end,
  on_cost = function(self, event, target, player, data)
    local to = player.room:askForChoosePlayers(player, table.map(player.room.alive_players, Util.IdMapper), 1, 1, "#ev__tianhuo", self.name, true)
    if #to > 0 then
      self.cost_data = to[1]
      return true
    end 
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(self.cost_data)
    room:setBanner("ev__tianhuo", {})
    room:useVirtualCard("blazing_flame", nil, to, player, self.name, true)
    if not player.dead and not to.dead then
      room:useVirtualCard("blazing_flame", nil, player, to, self.name, true)
      local cards = room:getBanner("ev__tianhuo")
      if type(cards) == "table" and #cards == 2 and Fk:getCardById(cards[1]).color == Fk:getCardById(cards[2]).color then
        local p = room:askForChoosePlayers(player, table.map(room.alive_players, Util.IdMapper), 1, 1, "#tianhuo-get", self.name, true)
        if player ~= room:getPlayerById(p[1]) then
          --room:getPlayerById(p[1]):addFakeSkill(self.name)
          room:handleAddLoseSkills(room:getPlayerById(p[1]), "ev__tianhuo")
          room:addPlayerMark(room:getPlayerById(p[1]), "ev__tianhuo")
        end
      end
    end
  end,

  refresh_events = {fk.TurnEnd, fk.AfterCardsMove},
  can_refresh = function(self, event, target, player, data)
    if event == fk.TurnEnd then
      return target == player and player:hasSkill(self.name) and player:getMark("ev__tianhuo") > 0
    else
      if player:hasSkill(self.name) then
        for _, move in ipairs(data) do
          local parentUseEvent = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard, true)
          if parentUseEvent then
            local useData = parentUseEvent.data[1]
            if useData.card.name == "blazing_flame" and useData.card.skillName == self.name then
              for _, info in ipairs(move.moveInfo) do
                if move.skillName == "blazing_flame" and move.moveReason == fk.ReasonDiscard then
                  return true
                end
              end
            end
          end
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    if event == fk.TurnEnd then
      player.room:handleAddLoseSkills(player, "-ev__tianhuo")
    else
      local ids = player.room:getBanner("ev__tianhuo")
      if type(ids) ~= "table" then ids = {} end
      for _, move in ipairs(data) do
        for _, info in ipairs(move.moveInfo) do
          if move.skillName == "blazing_flame" and move.moveReason == fk.ReasonDiscard then
            table.insertIfNeed(ids, info.cardId)
          end
        end
      end
    end
  end,
}

ev__dayaopower:addRelatedSkill(ev__dayaopower_slash)
guiwangjiutun:addSkill(ev__dayaopower)
guiwangjiutun:addSkill(ev__tianhuo)

Fk:loadTranslationTable{
  ["ev__guiwangjiutun"] = "鬼王酒吞",
  ["designer:ev__guiwangjiutun"] = "次氯酸",
  ["ev__dayaopower"] = "大妖之力",
  [":ev__dayaopower"] = "锁定技，若己方角色的体力值为1，则普通【杀】对其造成的伤害-1；若己方角色已损失的体力值为1，则其使用【杀】的次数上限+1。",
  ["ev__tianhuo"] = "天火怒焱",
  [":ev__tianhuo"] = "阶段技（始），你可令一名角色视为对你使用【炽焰】，然后你视为对其使用【炽焰】；当这两张牌结算结束后，若你与其因这两张【炽焰】弃置手牌的效果而弃置的两张牌颜色相同，你令一名角色于其下回合内获得“天火怒焱”。",
  ["#ev__tianhuo"] = "天火怒焱：你可令一名角色视为对你使用【炽焰】",
  ["#tianhuo-get"] = "天火怒焱：你可令一名角色获得此技能",
}

local season = General(extension, "ev__season", "ev", 3, 3, General.Female)

local function getSeason(num)
  local suit
  if num == 1 then
    suit = "spade"
  elseif num == 2 then
    suit = "heart"
  elseif num == 3 then
    suit = "diamond"
  elseif num == 4 then
    suit = "club"
  else
    suit = nil
  end
  return suit
end

local sijiliuzhuan = fk.CreateTriggerSkill{
  name = "sijiliuzhuan",
  anim_type = "special",
  events = {fk.TurnEnd},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      local room = player.room
      --if player:getMark("season") == 0 then room:setPlayerMark(player, "season", 1) end
      local changestate = player:getMark("season")
      local suit = getSeason(changestate)
      local ids = EV.getCenterArea(room)
      if #ids > 0 then
        return table.find(ids, function (id) return Fk:getCardById(id):getSuitString() == suit end)
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local unshown = table.filter(player:getCardIds(Player.Hand), function(id) return Fk:getCardById(id):getMark("@@shown-inhand") == 0 end)
    local cards = player.room:askForCard(player, 1, 1, false, self.name, true, tostring(Exppattern{ id = unshown }), "#sijiliuzhuan")
    if #cards > 0 then
      self.cost_data = cards[1]
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local changestate = player:getMark("season")
    local suit = getSeason(changestate)
    EV.OpenCards(room, player, self.cost_data, self.name)
    changestate = changestate + 1
    if changestate > 4 then
      changestate = 1
    end
    room:setPlayerMark(player, "season", changestate)
    room:setPlayerMark(player, "@season", "log_" .. getSeason(changestate))
    --local suit = Fk:getCardById(self.cost_data).suit
    if #player:getTableMark("@$shown") == player:getHandcardNum()
    and table.find(player:getCardIds(Player.Hand), function (id) return Fk:getCardById(id):getSuitString() == suit end) then
      player:drawCards(1, self.name)
    end
  end,

  refresh_events = {fk.EventAcquireSkill},
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and data.name == self.name
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "season", 1)
    player.room:setPlayerMark(player, "@season", "log_spade")
  end,
}

local hualuosishi = fk.CreateTriggerSkill{
  name = "hualuosishi",
  mute = true,
  events = {fk.DamageInflicted, fk.Damage},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self.name) and player:usedSkillTimes(self.name) == 0 then
      if event == fk.DamageInflicted then
        return target == player and table.find(player:getCardIds(Player.Hand), function(id) return Fk:getCardById(id):getMark("@@shown-inhand") > 0 end)
      elseif event == fk.Damage then
        return target ~= player and data.card and not data.to.dead
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.DamageInflicted then
      return player.room:askForSkillInvoke(player, self.name, nil, "#hualuosishi-defense")
    else
      local cards = table.filter(player:getCardIds("h"), function(id)
        return Fk:getCardById(id):getMark("@@shown-inhand") > 0 and Fk:getCardById(id).suit == data.card.suit end)
      if #cards > 0 then
        local id = player.room:askForCard(player, 1, 1, false, self.name, true, ".|.|.|.|.|.|" .. table.concat(cards, ","), "#hualuosishi-discard::"..data.to.id)
        if #id > 0 then
          self.cost_data = id[1]
          return true
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.DamageInflicted then
      local cards = table.filter(player:getCardIds(Player.Hand), function(id) return Fk:getCardById(id):getMark("@@shown-inhand") > 0 end)
      EV.ConcealCards(player.room, player, cards, self.name)
      room:notifySkillInvoked(player, self.name, "defensive")
      return true
    else
      room:notifySkillInvoked(player, self.name, "offensive")
      room:moveCardTo(self.cost_data, Card.DiscardPile, nil, fk.ReasonDiscard, self.name, nil, true, player.id)
      if not data.to.dead then
        room:loseHp(data.to, 1, self.name)
      end
    end
  end,
}

sijiliuzhuan.CenterArea = true
season:addSkill(sijiliuzhuan)
season:addSkill(hualuosishi)

Fk:loadTranslationTable{
  ["ev__season"] = "季",
  ["designer:ev__season"] = "次氯酸",
  --["illustrator:ev__season"] = "柠檬精今天份的吹爆",
  ["sijiliuzhuan"] = "四季流转",
  [":sijiliuzhuan"] = "转换技，一名角色的回合结束时，若于此回合内有①黑桃②红桃③方块④梅花牌置入过弃牌堆，你可明置一张手牌，然后若你的手牌均已明置且含有此花色，则你摸一张牌。",
  ["#sijiliuzhuan"] = "你可明置一张手牌",
  ["hualuosishi"] = "华落四时",
  [":hualuosishi"] = "回合技，当你受到伤害时，你可暗置所有手牌，防止此伤害。其他角色使用牌造成伤害后，你可弃置与此牌花色相同的一张明置手牌，令受伤角色失去1点体力。",
  ["#hualuosishi-defense"] = "你可暗置所有手牌，防止此伤害",
  ["#hualuosishi-discard"] = "你可令%dest失去1点体力",
  ["@season"] = "季节",
}
--
local nilu = General(extension, "ev__nilu", "ev", 3, 3, General.Female)

local liuguangpianxuan = fk.CreateTriggerSkill{
  name = "liuguangpianxuan",
  frequency = Skill.Compulsory,
  events = {fk.TargetSpecified},
  can_trigger = function(self, event, target, player, data)
    if player ~= target or player.phase == Player.NotActive or not player:hasSkill(self) then return false end
    local room = player.room
    local logic = room.logic
    local use_event = logic:getCurrentEvent()
    local events = logic.event_recorder[GameEvent.UseCard] or Util.DummyTable
    local last_find = false
    for i = #events, 1, -1 do
      local e = events[i]
      if player.phase ~= Player.NotActive then
        if e.id == use_event.id then
          last_find = true
        elseif last_find then
          local last_use = e.data[1]
          return data.card.type ~= last_use.card.type
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = room:askForCard(player, 1, 1, false, self.name, true, ".", "#liuguangpianxuan")
    if #cards > 0 then
      local card = Fk:getCardById(cards[1])
      if card:getMark("@@shown-inhand") == 0 then
        EV.OpenCards(room, player, cards, self.name)
        data.unoffsetableList = table.map(room.alive_players, Util.IdMapper)
      else
        room:recastCard(cards, player, self.name)
      end
    end
  end,

  refresh_events = {fk.AfterCardUseDeclared, fk.EventLoseSkill},
  can_refresh = function(self, event, target, player, data)
    if event == fk.AfterCardUseDeclared then
      return player:hasSkill(self, true) and player.phase ~= Player.NotActive
    elseif event == fk.EventLoseSkill then
      return data == self
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if event == fk.AfterCardUseDeclared then
      room:setPlayerMark(player, "@liuguangpianxuan-turn", {data.card:getTypeString()})
    elseif event == fk.EventLoseSkill then
      room:setPlayerMark(player, "@liuguangpianxuan-turn", 0)
    end
  end,
}

local liuguangpianxuan_tar = fk.CreateTargetModSkill{
  name = "#liuguangpianxuan_targetmod",
  bypass_times = function(self, player, skill, scope, card, to)
    return player:hasSkill(self) and card and not table.contains(player:getTableMark("@liuguangpianxuan-turn"), card:getTypeString())
  end,
  bypass_distances = function(self, player, skill, card, to)
    return player:hasSkill(self) and card and not table.contains(player:getTableMark("@liuguangpianxuan-turn"), card:getTypeString())
  end,
}

local guanzhonglaixin = fk.CreateTriggerSkill{
  name = "guanzhonglaixin",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player.phase == Player.Finish
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:drawCards(1, self.name)
    local shown = table.filter(player:getCardIds(Player.Hand), function(id) return Fk:getCardById(id):getMark("@@shown-inhand") > 0 end)
    if #shown > 0 then
      EV.ConcealCards(room, player, shown, self.name)
      local suits = {}
      for _, id in ipairs(shown) do
          local suit = Fk:getCardById(id):getSuitString()
          table.insertIfNeed(suits, suit)
      end
      for _, p in ipairs(room:getOtherPlayers(player)) do
        if not p.dead then
          local card = room:askForCard(p, 1, 1, false, self.name, true, ".|.|" .. table.concat(suits, ",") .. "|hand", "#guanzhonglaixin::"..player.id)
          if #card > 0 then
            room:moveCardTo(card, Card.PlayerHand, player, fk.ReasonPrey, self.name, "", true)
          end
        end
      end
    end
  end,
}

liuguangpianxuan:addRelatedSkill(liuguangpianxuan_tar)
nilu:addSkill(liuguangpianxuan)
nilu:addSkill(guanzhonglaixin)

Fk:loadTranslationTable{
  ["ev__nilu"] = "妮露",
  ["designer:ev__nilu"] = "幽蝶化烬",
  ["illustrator:ev__nilu"] = "",
  ["liuguangpianxuan"] = "流光翩旋",
  [":liuguangpianxuan"] = "锁定技，你的回合内，你使用与上一张被使用的牌类别不同的牌无距离与次数限制，且当你使用此牌指定目标后，你选择一项：1.明置一张手牌，令此牌不是牌的合法目标；2.重铸一张明置手牌。",
  ["@liuguangpianxuan-turn"] = "流光",
  ["#liuguangpianxuan"] = "你可以选择一张手牌，暗置则明置，否则重铸",
  ["guanzhonglaixin"] = "观众来信集",
  [":guanzhonglaixin"] = "结束阶段，你可摸一张牌，暗置所有手牌，然后其他角色各可交给你一张与以此法暗置的手牌花色相同的手牌。",
  ["#guanzhonglaixin"] = "你可以交给%dest一张与暗置牌花色相同的手牌",

  ["$liuguangpianxuan1"] = "和浪花共舞吧。",
  ["$liuguangpianxuan2"] = "这支舞，献给你。",
  ["$guanzhonglaixin1"] = "要不要来当我今天的第一位观众？",
  ["$guanzhonglaixin2"] = "我来帮忙一起收吧。",
  ["~ev__nilu"] = "美丽之物，不要远去…",
}

local qin = General(extension, "ev__qin", "ev", 4, 4, General.Female)

local fengyajian_distance = fk.CreateDistanceSkill{
  name = "#jfengyajian_distance",
  correct_func = function(self, from)
    if from:hasSkill(self) and from:getMark("fengyajian-turn") > 0 then
      return -1
    end
  end,
}

local fengyajian = fk.CreateActiveSkill{
  name = "fengyajian",
  anim_type = "offensive",
  prompt = "请选择两名有牌的角色，先选的先被杀",
  target_num = 2,
  card_num = 0,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function (self, to_select, selected, selected_cards, card, extra_data, player)
    local to = Fk:currentRoom():getPlayerById(to_select)
    if #selected == 0 then
      return to_select ~= player.id and not to:isNude()
      and player:inMyAttackRange(to, 1)
    else
      return not to:isNude()
    end
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local to = room:getPlayerById(effect.tos[1])
    room:addPlayerMark(player, "fengyajian-turn")
    local subcards = {}
    for _, p in ipairs(effect.tos) do
      local id = room:askForCardChosen(player, room:getPlayerById(p), "he", self.name)
      if id then
        table.insert(subcards, id)
      end
    end
    room:useVirtualCard("ev__slash", subcards, player, to, self.name, true)
    if to:isAlive() then
      room:moveCardTo(subcards, Card.PlayerHand, to, fk.ReasonJustMove, self.name, "", true)
    end
  end,
  target_tip = function (self, to_select, selected, selected_cards, card, selectable, extra_data)
    if #selected == 0 then
      return "#fengyajian-to"
    end
  end,
}


local tingpinfengyin = fk.CreateActiveSkill{
  name = "tingpinfengyin",
  anim_type = "offensive",
  card_num = 0,
  min_target_num = 1,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function (self, to_select, selected, selected_cards, card, extra_data, player)
    local to = Fk:currentRoom():getPlayerById(to_select)
    return player:distanceTo(to) <= 1
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local judge = {
      who = player,
      reason = self.name,
      pattern = ".",
    }
    room:judge(judge)
    local suit = judge.card.suit
    for _, p in ipairs(effect.tos) do
      local target = room:getPlayerById(p)
      local invoke = false
      local cards = table.filter(target:getCardIds("hej"), function (id) return Fk:getCardById(id).suit == suit end)
      if #cards > 0 then
        local ids, choice = U.askforChooseCardsAndChoice(target, cards, {"OK"}, self.name, "#tingpinfengyin-discard", {"Cancel"})
        if #ids > 0 and choice ~= "cancel" then
          invoke = true
          room:moveCardTo(ids, Card.DiscardPile, nil, fk.ReasonDiscard, self.name, nil, true)
          if room:askForSkillInvoke(player, self.name, nil, "#tingpinfengyin-invoke::"..target.id) then
            room:useVirtualCard("holy_blessing", nil, player, target, self.name, false)
          end
        end
      end
      if not invoke then
        local unshown = table.filter(target:getCardIds(Player.Hand), function(id) return Fk:getCardById(id):getMark("@@shown-inhand") == 0 end)
        if #unshown > 0 then
        local card = room:askForCard(target, 1, 1, false, self.name, false, tostring(Exppattern{ id = unshown }), "#tingpinfengyin-show")
          EV.OpenCards(room, target, card, self.name, target)
        end
      end
    end
  end,
}

fengyajian:addRelatedSkill(fengyajian_distance)
qin:addSkill(fengyajian)
qin:addSkill(tingpinfengyin)

Fk:loadTranslationTable{
  ["ev__qin"] = "琴",
  ["designer:ev__qin"] = "陆丿伯言",
  ["illustrator:ev__qin"] = "JIN",
  ["fengyajian"] = "风压剑",
  [":fengyajian"] = "阶段技，你可令你于本回合内与其他角色的距离-1，将两名角色的各一张牌当【杀】对其中一名角色使用；当此【杀】结算结束后，其获得这两张牌。",
  ["#fengyajian-to"] = "杀的目标",
  ["tingpinfengyin"] = "听凭风引",
  [":tingpinfengyin"] = "阶段技，你可判定，令距离1以内的任意名角色各选择一项：1.明置一张手牌；2.弃置其区域里与结果花色相同的一张牌，然后你可视为对其使用【神圣祝福】。",
  ["#tingpinfengyin-show"] = "你可以明置一张手牌",
  ["#tingpinfengyin-discard"] = "你可以弃置一张与结果花色相同的牌",
  ["#tingpinfengyin-invoke"] = "你可以视为对%dest使用【神圣祝福】",

  ["$fengyajian1"] = "一决胜负！",
  ["$fengyajian2"] = "以剑为誓！",
  ["$tingpinfengyin1"] = "风之神，请指引我们。",
  ["$tingpinfengyin2"] = "风，回应我吧。",
  ["~ev__qin"] = "我。。还不够称职。。",
}

local wushenglinglu = General(extension, "ev__wushenglinglu", "ev", 3, 3, General.Female)

local xiantoufengjing = fk.CreateTriggerSkill{
  name = "xiantoufengjing",
  anim_type = "offensive",
  events = {fk.RoundStart},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local ids = room:getNCards(player.maxHp)
    room:moveCardTo(ids, Card.Processing, nil, fk.ReasonJustMove, self.name)
    local n = math.min(room:getTag("RoundCount"), #ids)
    --耦合
    if player:hasSkill("transforming") then
      local cards = table.filter(ids, function (id)
        return table.contains(player:getTableMark("@transforming"), Fk:getCardById(id):getSuitString(true))
      end)
      if #cards > 0 then
        local choice = U.askforChooseCardsAndChoice(player, cards, {"OK"}, self.name, "请选择获得的牌", {"Cancel"}, 1, 1, ids)
        if choice ~= "Cancel" then
          table.removeOne(ids, choice[1])
          room:moveCardTo(choice[1], Card.PlayerHand, player, fk.ReasonJustMove, self.name, nil, true)
        end
      end
    end
    local suits = {}
    while n > 0 do
      if table.every(ids, function (id)
        return not U.canUseCard(room, player, Fk:getCardById(id), false)
      end) then break end
      local use = U.askForUseRealCard(room, player, ids, ".", self.name, "#xiantoufengjing-use:::"..n,
      {expand_pile = ids, bypass_times = true}, false, false)
      if use then
        table.removeOne(ids, use.card:getEffectiveId())
        if table.contains(suits, use.card:getSuitString()) then
          room:addPlayerMark(player, "xiantoufengjin-suit")
        else
          table.insert(suits, use.card:getSuitString())
        end
        n = n - 1
      end
    end
    if #ids > 0 then
      U.clearRemainCards(room, ids, self.name)
    end
    if player:getMark("xiantoufengjin-suit") > 0 then
      room:loseHp(player, room:getTag("RoundCount"))
    end
    room:setPlayerMark(player, "xiantoufengjin-suit", 0)
  end,
}

local transforming = fk.CreateTriggerSkill{
  name = "transforming",
  anim_type = "drawcard",

  refresh_events = {fk.TargetSpecified},
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(self) and target ~= player and EV.isFriend(player, target) and #AimGroup:getAllTargets(data.tos) > 0
    and table.contains(AimGroup:getAllTargets(data.tos), player.id) and data.card.suit ~= Card.NoSuit
  end,
  on_refresh = function(self, event, target, player, data)
    local mark = player:getTableMark("@transforming")
    table.insertIfNeed(mark, data.card:getSuitString(true))
    player.room:setPlayerMark(player, "@transforming", mark)
  end,
}

wushenglinglu:addSkill(xiantoufengjing)
wushenglinglu:addSkill(transforming)

Fk:loadTranslationTable{
  ["ev__wushenglinglu"] = "无声铃鹿",
  ["designer:ev__wushenglinglu"] = "KINGDOM43",
  ["xiantoufengjing"] = "先头风景",
  [":xiantoufengjing"] = "每轮开始时，你可亮出牌堆顶的等同于体力上限张牌，依次使用其中的X张牌；然后若你使用的这些牌中有至少两张花色相同，你失去X点体力（X为轮数）。",
  ["#xiantoufengjing-use"] = "先头风景：请使用其中的一张牌，还需使用%arg张",
  ["transforming"] = "Transforming",
  ["@transforming"] = "已记录",
  [":transforming"] = "其他己方角色使用牌指定你为目标后，你记录此花色；当你因“先头风景”而亮出牌后，你可获得其中已记录花色的一张牌。",
	
  ["$xiantoufengjing"] = "（日）这是……只属于我的景色！",
  ["$transforming1"] = "（日）向前奔跑。",
  ["$transforming2"] = "（日）不能满足于此！",
  ["~ev__wushenglinglu"] = "（日）不知道为什么，双腿好沉重……",
}

local xiaogong = General(extension, "ev__xiaogong", "ev", 4, 4, General.Female)

local ev__yanxiao = fk.CreateViewAsSkill{
  name = "ev__yanxiao",
  anim_type = "offensive",
  pattern = "blazing_flame",
  card_filter = function(self, to_select, selected)
    return Fk:getCardById(to_select).type == Card.TypeBasic
  end,
  view_as = function(self, cards)
    if #cards == 0 then
      return nil
    end
    local c = Fk:cloneCard("blazing_flame")
    c.skillName = self.name
    c:addSubcards(cards)
    return c
  end,
  enabled_at_play = function (self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end
}

local yanxiao_targetmod = fk.CreateTargetModSkill{
  name = "#ev__yanxiao_targetmod",
  extra_target_func = function(self, player, skill, card)
    if skill.trueName == "blazing_flame_skill" and player:hasSkill("ev__yanxiao") and card and card.skillName == "ev__yanxiao" then
      return #card.subcards
    end
  end,
}

local yanxiao_trigger = fk.CreateTriggerSkill{
  name = "#yanxiao_trigger",
  mute = true,
  events = {fk.TargetSpecifying},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self.name) and data.firstTarget and data.card.trueName == "blazing_flame" and #AimGroup:getAllTargets(data.tos) > 0
  end,
  on_cost = function(self, event, target, player, data)
    local tos = player.room:askForChoosePlayers(player, AimGroup:getAllTargets(data.tos), 1, #AimGroup:getAllTargets(data.tos), "#ev__yanxiao-choose", self.name, true)
    if #tos > 0 then
      self.cost_data = tos
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local n = #self.cost_data
    local cards = room:getNCards(n)
    room:moveCardTo(cards, Card.Processing, nil, fk.ReasonJustMove, self.name)
    for _, id in ipairs(self.cost_data) do
      AimGroup:cancelTarget(data, id)
      local p = room:getPlayerById(id)
      local ids, _ = U.askforChooseCardsAndChoice(p, cards, {"OK"}, "ev__xiaogong", "#ev__yanxiao")
      room:moveCardTo(ids, Card.PlayerHand, p, fk.ReasonJustMove, "ev__xiaogong", "", true)
      table.removeOne(cards, ids[1])
    end
    if #cards > 0 then
      room:moveCardTo(cards, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, "ev__xiaogong")
    end
  end,
}

local ev__liujin = fk.CreateTriggerSkill{
  name = "ev__liujin",
  mute = true,
  frequency = Skill.Limited,
  events = {fk.TurnStart, fk.TurnEnd},
  can_trigger = function(self, event, target, player, data)
    if event == fk.TurnStart then
      return target ~= player and player:hasSkill(self.name) and player:usedSkillTimes(self.name, Player.HistoryGame) == 0
    else
      return player:hasSkill(self.name) and player:usedSkillTimes(self.name, Player.HistoryGame) > 0 and player:getMark("ev__liujin-turn") ~= 0
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if event == fk.TurnStart then
      local targets = table.map(room.alive_players, Util.IdMapper)
      local tos = room:askForChoosePlayers(player, targets, 1, 1, "#ev__liujin", self.name, true)
      if #tos > 0 then
        self.cost_data = tos[1]
        return true
      end
    else
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.TurnStart then
      room:notifySkillInvoked(player, self.name)
      player:broadcastSkillInvoke(self.name)
      room:setPlayerMark(player, "ev__liujin-turn", self.cost_data)
    else
      local to = room:getPlayerById(player:getMark("ev__liujin-turn"))
      local num = 0
      room.logic:getEventsOfScope(GameEvent.ChangeHp, 999, function (e)
        local damage = e.data[5]
        if damage and to == damage.to then
          num = num + damage.damage
        end
      end, Player.HistoryTurn)
      room:notifySkillInvoked(player, self.name, "drawcard")
      player:drawCards(num, self.name)
      room:useVirtualCard("blazing_flame", nil, player, to, self.name, false)
    end
  end,
}

ev__yanxiao:addRelatedSkill(yanxiao_targetmod)
ev__yanxiao:addRelatedSkill(yanxiao_trigger)
xiaogong:addSkill(ev__yanxiao)
xiaogong:addSkill(ev__liujin)
Fk:loadTranslationTable{
  ["ev__xiaogong"] = "宵宫",
  ["designer:ev__xiaogong"] = "陆丿伯言",
  ["ev__yanxiao"] = "庭火焰硝",
  ["#yanxiao_trigger"] = "庭火焰硝",
  [":ev__yanxiao"] = "阶段技，你可将至少一张基础牌当【炽焰】使用，且此【炽焰】可额外选择等量个目标。当你使用【炽焰】指定目标时，你可取消此牌的任意个目标并亮出牌堆顶的等量张牌，然后这些角色各获得其中的一张牌。",
  ["#ev__yanxiao-choose"] = "你可取消此牌的任意个目标",
  ["#ev__yanxiao"] = "请选择其中一张牌获得之",
  ["ev__liujin"] = "琉金火光",
  [":ev__liujin"] = "限定技，其他角色的回合开始时，你可选择一名角色，此回合结束时，你摸X张牌，视为对其使用【炽焰】（X为其于此回合内受到过的伤害数）。",
  ["#ev__liujin"] = "琉金火光：你可选择一名角色",

  ["$ev__yanxiao1"] = "想看烟花吗？",
  ["$ev__yanxiao2"] = "点燃引信！",
  ["$ev__liujin1"] = "小心火烛~",
  ["$ev__liujin2"] = "烟花来喽！",
  ["~ev__xiaogong"] = "祭典…结束了。",
}

local yelan = General(extension, "ev__yelan", "ev", 4, 4, General.Female)

local ev__weixian = fk.CreateActiveSkill{
  name = "ev__weixian",
  anim_type = "offensive",
  prompt = "请选择一半暗置手牌和一名角色",
  target_num = 1,
  card_num = function(self)
    local unshown = table.filter(Self:getCardIds(Player.Hand), function(id) return Fk:getCardById(id):getMark("@@shown-inhand") == 0 end)
    if #unshown > 0 then
      return math.ceil(#unshown / 2)
    end
    return 999
  end,
  can_use = function(self, player)
    return player:getMark("@@ev__weixian-turn") == 0 and not player:isKongcheng()
  end,
  card_filter = function(self, to_select, selected)
    return Fk:getCardById(to_select):getMark("@@shown-inhand") == 0 and Fk:currentRoom():getCardArea(to_select) == Card.PlayerHand
  end,
  target_filter = function(self, to_select, selected)
    return #selected == 0 and Fk:currentRoom():getPlayerById(to_select):getMark("ev__weixian-turn") == 0
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    room:addPlayerMark(target, "ev__weixian-turn")
    EV.OpenCards(room, player, effect.cards, self.name)
    local num = math.min(player:getHandcardNum(), 4)
    local choices = {}
    for i = 1, num do
      table.insert(choices, tostring(i))
    end
    local choice = room:askForChoice(target, choices, self.name, "#ev__weixian::"..effect.from)
    EV.choicelog(target, choice)
    if EV.countSuits(player:getCardIds(Player.Hand)) == tonumber(choice) then
      local id = player:drawCards(1, self.name)
      EV.OpenCards(room, player, id, self.name)
      room:addPlayerMark(player, "@@ev__weixian-turn")
    else
      target:setChainState(true)
    end
  end,
}

local ev__dingju = fk.CreateViewAsSkill{
  name = "ev__dingju",
  prompt = "选择明置或暗置及杀的目标，无需选牌",
  pattern = "slash",
  interaction = function()
    local names = {}
    if table.find(Self:getCardIds(Player.Hand), function(id) return Fk:getCardById(id):getMark("@@shown-inhand") == 0 end) then
      table.insert(names, "unshown")
    end
    if table.find(Self:getCardIds(Player.Hand), function(id) return Fk:getCardById(id):getMark("@@shown-inhand") > 0 end) then
      table.insert(names, "shown")
    end
    if #names == 0 then return end
    return UI.ComboBox {choices = names}
  end,
  card_filter = Util.FalseFunc,
  view_as = function(self, cards)
    if #cards ~= 0 or not self.interaction.data then return end
    local unshown = table.filter(Self:getCardIds(Player.Hand), function(id) return Fk:getCardById(id):getMark("@@shown-inhand") == 0 end)
    local shown = table.filter(Self:getCardIds(Player.Hand), function(id) return Fk:getCardById(id):getMark("@@shown-inhand") > 0 end)
    local card = Fk:cloneCard("ev_ice__slash")
    card.skillName = self.name
    if self.interaction.data == "unshown" then
      card:addSubcards(unshown)
    elseif self.interaction.data == "shown" then
      card:addSubcards(shown)
    end
    return card
  end,
  enabled_at_play = function(self, player)
    return player:usedSkillTimes(self.name) == 0 and not player:isKongcheng() 
  end,
  enabled_at_response = function(self, player, response)
    return player:usedSkillTimes(self.name) == 0 and not player:isKongcheng() and not response
  end,
}

local dingju_trigger = fk.CreateTriggerSkill{
  name = "#dingju_trigger",
  mute = true,
  events = {fk.TargetSpecified},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill("ev__dingju") and data.card.trueName == "slash"
    and data.card.skillName == "ev__dingju" and not player.room:getPlayerById(data.to).chained
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    data.disresponsive = true
  end,
}

local dingju_tar = fk.CreateTargetModSkill{
  name = "#ev__dingju_targetmod",
  distance_limit_func =  function(self, player, skill, card)
    if card and card.trueName == "slash" and card.skillName == "ev__dingju" then
      return 999
    end
  end,
}
ev__dingju:addRelatedSkill(dingju_tar)
ev__dingju:addRelatedSkill(dingju_trigger)
yelan:addSkill(ev__weixian)
yelan:addSkill(ev__dingju)

Fk:loadTranslationTable{
  ["ev__yelan"] = "夜兰",
  ["designer:ev__yelan"] = "re",
  ["ev__weixian"] = "命系危弦",
  [":ev__weixian"] = "出牌阶段，你可明置一半暗置手牌（向上取整），令于本回合内未以此法选择过的一名其他角色声明你手牌含有的花色数；若此声明正确/错误，你摸一张牌并明置之，令此技能于本回合内失效/令其横置。",
  ["#ev__weixian"] = "请猜测%dest手牌含有的花色数",
  ["@@ev__weixian-turn"] = "命系危弦失效",
  ["ev__dingju"] = "一矢定局",
  [":ev__dingju"] = "回合技，你可将所有明置或暗置手牌当无距离限制的冰【杀】使用；当此冰【杀】指定一个目标后，若其未横置，其不能对此冰【杀】使用【闪】。",
  ["shown"] = "明置",
  ["unshown"] = "暗置",

  ["$ev__weixian1"] = "抓到了。",
  ["$ev__weixian2"] = "别喊疼。",
  ["$ev__dingju1"] = "终场~收工！",
  ["$ev__dingju2"] = "丝线，交织。",
  ["~ev__yelan"] = "真相…还没揭晓…",
}
return extension